<template>
    <div>
        <div>
            <el-table
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column
                  prop="sellerId"
                  label="商家ID"
                  width="180">
                </el-table-column>
                <el-table-column
                  prop="name"
                  label="公司名称"
                  width="180">
                </el-table-column>
                <el-table-column
                  prop="nickName"
                  label="店铺名称">
                </el-table-column>
                <el-table-column
                  prop="legalPerson"
                  label="法人代表">
                </el-table-column>
                <el-table-column
                  prop="mobile"
                  label="移动电话">
                </el-table-column>
                <el-table-column
                  prop="address"
                  label="公司地址">
                </el-table-column>
                <el-table-column
                  prop="createTime"
                  :formatter="formatterTime"
                  label="注册时间">
                </el-table-column>
                <el-table-column
                  prop="status"
                  :formatter="formatterStatus"
                  label="商家状态">
                </el-table-column>
                <el-table-column label="操作">
                      <template slot-scope="scope">
                        <el-button
                          size="mini"
                          v-if="scope.row.status==0"
                          @click="handleCheckShow(scope.$index, scope.row)">审核</el-button>
                        <el-button
                          size="mini"
                          type="danger"
                          v-if="scope.row.status!=2"
                          @click="handleSellerLock(scope.$index, scope.row)">锁定</el-button>
                        <el-button
                          size="mini"
                          type="warning"
                          v-if="scope.row.status==2">解锁</el-button>
                      </template>
                </el-table-column>
              </el-table>
        </div>
        <div class="block">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageInfo.pageNum"
              :page-sizes="[4, 6, 8, 10]"
              :page-size="pageInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
        </div>
        <div v-if="sellerInfo!=null">
            <el-dialog title="商家审核" :visible.sync="checkDialogVisible">
              <div>
                  <el-descriptions title="垂直带边框列表" direction="vertical" :column="4" border>
                    <el-descriptions-item label="商家ID">{{sellerInfo.sellerId}}</el-descriptions-item>
                    <el-descriptions-item label="公司名称">{{sellerInfo.name}}</el-descriptions-item>
                    <el-descriptions-item label="店铺名称">{{sellerInfo.nickName}}</el-descriptions-item>
                    <el-descriptions-item label="法人代表">{{sellerInfo.legalPerson}}</el-descriptions-item>
                    <el-descriptions-item label="移动电话">{{sellerInfo.mobile}}</el-descriptions-item>
                    <el-descriptions-item label="公司地址">{{sellerInfo.address}}</el-descriptions-item>
                    <el-descriptions-item label="公司地址">{{sellerInfo.address}}</el-descriptions-item>
                    <el-descriptions-item label="注册时间">{{sellerInfo.createTime}}</el-descriptions-item>
                  </el-descriptions>
              </div>
              <div slot="footer" class="dialog-footer">
                <el-button @click="checkDialogVisible = false">关闭窗口</el-button>
                <el-button type="primary" @click="handleCheckPass(1)">审核通过</el-button>
                <el-button type="primary" @click="handleCheckPass(-1)">审核不通过</el-button>
              </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import {getSellerByPage,sellerCheck,sellerLock} from '../api/api.js'
    export default {
        name: "SellerList",
        data() {
            return {
                tableData: [],
                pageInfo: {
                    pageNum: 1,
                    pageSize: 4
                },
                total: 0,
                checkDialogVisible: false,
                // 需要审核的商家ID
                sellerId: 0,
                sellerInfo: null
            }
        },
        mounted() {
            this.loadSellerList()
        },
        methods: {
            loadSellerList() {
                // 分页获取卖家列表
                getSellerByPage(this.pageInfo).then(res => {
                    console.log('getSellerByPage', res);
                    if(res.data.code == 200) {
                        this.tableData = res.data.data.records
                        this.total = res.data.data.total
                    }
                    else {
                        let msg = res.data.msg
                        this.$message.error(msg)
                    }
                })
            },
            formatterStatus(row) {
                if(row.status == 0) {
                    return '未审核'
                }
                else if(row.status == 1) {
                    return '已审核'
                }
                if(row.status == 2) {
                    return '已锁定'
                }
                else {
                    return '未知'
                }
            },
            formatterTime(row) {
                var date = new Date(row.createTime);
                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageInfo.pageSize = val
                this.loadSellerList()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.pageInfo.pageNum = val
                this.loadSellerList()
            },
            // 显示审核对话框
            handleCheckShow(index,row) {
                this.sellerId = row.sellerId
                this.sellerInfo = row
                console.log('handleCheckShow', row);
                this.checkDialogVisible = true
            },
            // 判断审核是否通过
            handleCheckPass(value) {
                this.checkDialogVisible = false
                let param = {
                    sellerId: this.sellerInfo.sellerId,
                    status: value
                }
                sellerCheck(param).then(res => {
                    if(res.data.code == 200) {
                        this.$message.success("操作成功")
                        this.loadSellerList()
                    }
                    else {
                        let msg = res.data.msg
                        this.$message.error(msg)
                    }
                })
                
                if(value == 1) {
                    console.log('审核通过');
                    
                }
                else {
                    console.log('审核不通过');
                }
            },
            // 商家锁定
            handleSellerLock(index, row) {
                let param = {
                    sellerId: row.sellerId,
                    status: 2
                }
                sellerLock(param).then(res => {
                    if(res.data.code == 200) {
                        this.$message.success("操作成功")
                        this.loadSellerList()
                    }
                    else {
                        let msg = res.data.msg
                        this.$message.error(msg)
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .block {
        margin-top: 40px;
    }
</style>
